package dynamicprogramming.完全平方数;

import java.util.Arrays;

public class Solution {
    public int numSquares(int n) {
        /**
         * f[j] 表示 J的最小完全平方数 ， f[j] = min(f[j - i * i]+1  , f[j])  1 < i < j^1/2
         */
        int[] f = new int[n + 1];
        for (int i = 0; i < f.length; i++) {
            f[i] = i;
        }
        for (int i = 0; i <= n; i++) {
            for (int j = 0; j <= Math.sqrt(i); j++) {
                f[i] = Math.min(f[i], f[i - j * j] + 1);
            }
        }
        return f[n];
    }

    public static void main(String[] args) {
        Solution solution = new Solution();
        System.out.println(solution.numSquares(13));
    }
}
